package tmproject.hlhj.fhp.cartrans.http

import com.google.gson.Gson
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import io.reactivex.disposables.Disposable
import rx.Observer
import tmproject.hlhj.fhp.cartrans.customVIews.LoadingDialog
import tmproject.hlhj.fhp.cartrans.javaBean.BaseBean
import tmproject.hlhj.fhp.cartrans.log
import tmproject.hlhj.fhp.cartrans.utils.LoadingUtil
import tmproject.hlhj.fhp.cartrans.utils.ToastUtils

/**
 * Created by Administrator on 2018/8/23/023.
 */
 interface BaseAciton<T> :io.reactivex.Observer<T>{

    override fun onError(e: Throwable) {
        LoadingUtil.hideLoading()
        ToastUtils.toast(e?.message!!)
        e?.printStackTrace()
        log("okgo","网络请求报错了 看栈${e?.toString()}")
        httpFailed(e)
    }
    override fun onComplete() {
        LoadingUtil.hideLoading()

    }

    override fun onNext(t: T) {
        if (t is Response<*>){
            if (!t.isSuccessful)return
            val body = t.body()
            val gsonStr = Gson().toJson(body)
            val baseBean = Gson().fromJson(gsonStr, BaseBean::class.java)
            if (baseBean.code==200){
                httpSuccess(t)
            }
            else{
                baseBean?.msg?.let {
                    ToastUtils.toast(it)
                }
                httpErroCode(baseBean)
            }
        }
    }

     fun httpSuccess(t:T)

     fun httpErroCode(baseBean: BaseBean)

     fun httpFailed(e:Throwable?)

}